package org.xtframe.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.xtframe.dao.DataBaseDao;
import org.xtframe.dao.DataBaseFactory;
import org.xtframe.entity.ServerData;
import org.xtframe.entity.UserData;
import org.xtframe.service.AjaxDataHandler;
import org.xtframe.service.AjaxDataHandlerException;
import org.xtframe.sql.SqlData;
import org.xtframe.util.CommonUtil;
import org.xtframe.util.ContextUtil;
import org.xtframe.vo.CheckVO;

@Service
@Scope("prototype")
public class ExecuteQueryTransactionDataHandler extends BasicExecute implements AjaxDataHandler {

	private final Log logger = LogFactory.getLog(getClass());

	public Map<String, Object> execute(String strData) {
		Map<String, Object> map = new HashMap<String, Object>();
		
		try {
			UserData userData = ContextUtil.getCurrentUserData();
			ServerData serverData = ContextUtil.getServerData();
			
			JSONObject objJson = JSONObject.fromObject(strData);
			JSONArray arrSqlValue = objJson.optJSONArray("arrSqlValue");
			if(arrSqlValue == null) throw new AjaxDataHandlerException("sqlID列表为空");
			
			ArrayList<SqlData> sqlList = new ArrayList<SqlData>();
			String sqlID = null;
			for(int i=0; i<arrSqlValue.size(); i++) {
				ArrayList<String> tempList = CommonUtil.JSONArrayToArrayList_String(arrSqlValue.optJSONArray(i));
				if(tempList.size() > 0){
					sqlID = tempList.get(0);
					tempList.remove(0);
					String arrValue = JSONArray.fromObject(tempList).toString();
					CheckVO checkVO = check(sqlID, arrValue, userData, serverData);
					switch(checkVO.getResult()){
						case CheckVO.SQLID_NULL:
							throw new AjaxDataHandlerException("sqlID为空");
						case CheckVO.SQLID_NO_RIGHT:
							map.put(STATUS, false);
							map.put(RIGHT, false);
							throw new AjaxDataHandlerException("不具有该sql("+sqlID+")的执行权限", 1);
						case CheckVO.SQLID_INEXISTENCE:
							throw new AjaxDataHandlerException("该sql("+sqlID+")不存在");
						case CheckVO.CHECK_FAILED:
							throw new AjaxDataHandlerException("sql("+sqlID+")数据格式校验失败");
					}
					sqlList.add(checkVO.getSqlData());
				}else{
					throw new AjaxDataHandlerException("sqlID为空");
				}
			}
			
			ArrayList<List<Map<String, Object>>> dataList = new ArrayList<List<Map<String, Object>>>();
			DataBaseDao dataBaseDao = DataBaseFactory.getDataBaseDaoForSqlId(sqlID);
			for(SqlData sql : sqlList){
				List<Map<String, Object>> result = dataBaseDao.executeQuery(sql.getSql(), sql.getPreparedParameters());
				if(result == null) throw new AjaxDataHandlerException("sql("+sql+")执行失败");
				dataList.add(result);
			}
			map.put(STATUS, true);
			map.put(LIST, dataList);
		} catch(AjaxDataHandlerException ex){
			logger.warn(ex.toString());
			if(ex.getFlag() == 0){
				map.clear();
				map.put(STATUS, false);
			}
		} catch (Exception e) {
			logger.error(e.toString());
			map.clear();
			map.put(STATUS, false);
		}
		
		return map;
	}

}
